
var res_mgr = require("res_mgr");

var EFFECT_NUM = 8; //节点上做多同时播放8个音效
var sound_mgr = cc.Class({
    extends: cc.Component,

    statics:{
        Instance:null,
    },  

    properties: {
     
    },

    onLoad () {
        // cc.log("========sound_mgr onLoad=============");
        if(sound_mgr.Instance === null){
            sound_mgr.Instance = this;
        }else{
            cc.error("sound_mgr has multi instances!!!");
            this.destroy();
        }

        //读取本地的参数0 是不静音 1 静音
        this.music_mute = cc.sys.localStorage.getItem("music_mute");
        this.music_mute =  (!this.music_mute) ? 0 : this.music_mute ; // 背景音乐是否静音
        this.effect_mute = cc.sys.localStorage.getItem("effect_mute");
        this.effect_mute =  (!this.effect_mute) ? 0 : this.effect_mute; // y音效是否静音

        this.music_volume = cc.sys.localStorage.getItem("music_volume");
        this.music_volume =  (!this.music_volume) ? 1 : this.music_volume; // 背景音乐音量
        this.effect_volume = cc.sys.localStorage.getItem("effect_volume");
        this.effect_volume =  (!this.effect_volume) ? 1 : this.effect_volume; // 音效音量
        //end

        //背景音乐
        this.music_as = this.node.addComponent(cc.AudioSource);
        this.music_as.mute = (this.music_mute  == 1);
        this.music_as.volume = this.music_volume;
        //音效
        this.effect_as = [];
        for (let i = 0; i < EFFECT_NUM; i++) {
            var as = this.node.addComponent(cc.AudioSource)
            this.effect_as.push(as);
            as.mute = (this.effect_mute  == 1);
            as.volume = this.effect_volume;
        }
        //调用索引
        this.cur_as = 0;
    },

    //获取背景音乐是否静音
    get_music_mute(){
        return this.music_mute;
    },
    //设置背景音乐是否静音
    set_music_mute(b_mule){
        var value = b_mule ? 1: 0;
        if(value == this.music_mute){
            return;
        }
        this.music_mute = b_mule;
        this.music_as.mute = b_mule;

        cc.sys.localStorage.setItem("music_mute",value);
    },

    //获取设置背景音乐音量大小
    get_music_volume(){
        return this.music_volume;
    },
    set_music_volume(value){
        this.music_volume = value;
        this.music_as.volume = value;

        cc.sys.localStorage.setItem("music_volume",value);
    },


     //获取音效是否静音
    get_effct_mute(){
        return this.effect_mute;
    },
    //设置音效是否静音
    set_effect_mute(b_mule){
        var value = b_mule ? 1: 0;
        if(value == this.effect_mute){
            return;
        }

        for (let i = 0; i < this.efeect_as.length; i++) {
            this.effect_as[i].mute = b_mule;
        }

        this.effect_mute = value;
        cc.sys.localStorage.setItem("effect_mute",value);
    },
    //获取设置音效音量大小
    get_effect_volume(){
        return this.effect_volume;
    },
    set_effect_volume(value){
        for (let i = 0; i < this.efeect_as.length; i++) {
            this.effect_as[i].volume = value;
        }

        this.effect_volume = value;
        cc.sys.localStorage.setItem("effect_volume",value);
    },

    play_music(url, loop){
        // cc.log("==========sound_mgr==play_music=========")
        var loop = (loop) ? true :false;
        this.music_as.loop = loop;

        this.music_as.clip = res_mgr.Instance.get_res(url);
        if(this.music_as.clip ){
            // cc.log("==========sound_mgr==play_music1=========", this.music_as.volume, this.music_as.mute);
            this.music_as.play();
        }else{
            cc.error("music audio clip null",url);
        }
    },

    stop_music(){
        this.music_as.stop();
    },

    play_effect(url){
        // cc.log("=======play_effect=========",this.effect_as);
        let as = this.effect_as[this.cur_as];
        this.cur_as++;
        if(this.cur_as >= EFFECT_NUM){
            this.cur_as = 0;
        }
        as.clip = res_mgr.Instance.get_res(url);
        if(as.clip){
            as.play();
        }else{
            cc.error("effect audio clip null",url);
        }
    },

    start () {

    },

    update (dt) {},
});
